#ifndef __Collections_List_H__
#define __Collections_List_H__

//+
#define _defaultCapacity 4

///<summary>
/// List<T>
///</summary>
//+ requires: System_Action(T)
//+ requires: System_Predicate(T)
//+ requires: System_Comparison(T)
//+ requires: Collections_IEnumerator(T)
//+ requires: Collections_IEnumerable(T)
//+ requires: Collections_IComparer(T)
#define Collections_List(T) bindT(Collections_List,T)
#define Declare_Collections_List(T, Ti) \
	public classT(Collections_List,T,System_Object,, \
		public Ti *Items; \
		public int_ Count; \
		public uint Version; \
	); \
	Declare_DefaultCtorT(public,Collections_List,T) \
	Declare_CtorT(2,public,Collections_List,T, Collections_IEnumerable(T) *collection) \
	Declare_CtorT(3,public,Collections_List,T, int_ capacity) \
	/* Forward-declare */ \
	public void spaceT(Collections_List,T,Add)(Collections_List(T) *t, Ti item); \
	public int_ spaceT(Collections_List,T,BinarySearch)(Collections_List(T) *t, Ti item); \
	public int_ spaceT(Collections_List,T,BinarySearch2)(Collections_List(T) *t, Ti item, Collections_IComparer(T) *comparer); \
	public int_ spaceT(Collections_List,T,BinarySearch3)(Collections_List(T) *t, int_ index, int_ count, Ti item, Collections_IComparer(T) *comparer); \
	public void spaceT(Collections_List,T,Clear)(Collections_List(T) *t); \
	public bool spaceT(Collections_List,T,Contains)(Collections_List(T) *t, Ti item); \
	public void spaceT(Collections_List,T,CopyTo)(Collections_List(T) *t, Ti array[]); \
	public void spaceT(Collections_List,T,CopyTo2)(Collections_List(T) *t, Ti array[], int_ arrayIndex); \
	public void spaceT(Collections_List,T,CopyTo3)(Collections_List(T) *t, int_ index, Ti array[], int_ arrayIndex, int_ count); \
	public bool spaceT(Collections_List,T,Exists)(Collections_List(T) *t, System_Predicate(T) match); \
	public Ti spaceT(Collections_List,T,Find)(Collections_List(T) *t, System_Predicate(T) match); \
	public Collections_List(T) *spaceT(Collections_List,T,FindAll)(Collections_List(T) *t, System_Predicate(T) match); \
	public int_ spaceT(Collections_List,T,FindIndex)(Collections_List(T) *t, System_Predicate(T) match); \
	public int_ spaceT(Collections_List,T,FindIndex2)(Collections_List(T) *t, int_ startIndex, System_Predicate(T) match); \
	public int_ spaceT(Collections_List,T,FindIndex3)(Collections_List(T) *t, int_ startIndex, int_ count, System_Predicate(T) match); \
	public Ti spaceT(Collections_List,T,FindLast)(Collections_List(T) *t, System_Predicate(T) match); \
	public int_ spaceT(Collections_List,T,FindLastIndex)(Collections_List(T) *t, System_Predicate(T) match); \
	public int_ spaceT(Collections_List,T,FindLastIndex2)(Collections_List(T) *t, int_ startIndex, System_Predicate(T) match); \
	public int_ spaceT(Collections_List,T,FindLastIndex3)(Collections_List(T) *t, int_ startIndex, int_ count, System_Predicate(T) match); \
	public void spaceT(Collections_List,T,ForEach)(Collections_List(T) *t, System_Action(T) action); \
	public Collections_List(T) *spaceT(Collections_List,T,GetRange)(Collections_List(T) *t, int_ index, int_ count); \
	public int_ spaceT(Collections_List,T,IndexOf)(Collections_List(T) *t, Ti item); \
	public int_ spaceT(Collections_List,T,IndexOf2)(Collections_List(T) *t, Ti item, int_ index); \
	public int_ spaceT(Collections_List,T,IndexOf3)(Collections_List(T) *t, Ti item, int_ index, int_ count); \
	public void spaceT(Collections_List,T,Insert)(Collections_List(T) *t, int_ index, Ti item); \
	public int_ spaceT(Collections_List,T,LastIndexOf)(Collections_List(T) *t, Ti item); \
	public int_ spaceT(Collections_List,T,LastIndexOf2)(Collections_List(T) *t, Ti item, int_ index); \
	public int_ spaceT(Collections_List,T,LastIndexOf3)(Collections_List(T) *t, Ti item, int_ index, int_ count); \
	public int_ spaceT(Collections_List,T,RemoveAll)(Collections_List(T) *t, System_Predicate(T) match); \
	public void spaceT(Collections_List,T,RemoveAt)(Collections_List(T) *t, int_ index); \
	public void spaceT(Collections_List,T,RemoveRange)(Collections_List(T) *t, int_ index, int_ count); \
	public void spaceT(Collections_List,T,Reverse)(Collections_List(T) *t); \
	public void spaceT(Collections_List,T,Reverse2)(Collections_List(T) *t, int_ index, int_ count); \
	public void spaceT(Collections_List,T,Sort)(Collections_List(T) *t); \
	public void spaceT(Collections_List,T,Sort2)(Collections_List(T) *t, Collections_IComparer(T) *comparer); \
	public void spaceT(Collections_List,T,Sort3)(Collections_List(T) *t, System_Comparison(T) comparison); \
	public void spaceT(Collections_List,T,Sort4)(Collections_List(T) *t, int_ index, int_ count, Collections_IComparer(T) *comparer); \
	public Ti *spaceT(Collections_List,T,ToArray)(Collections_List(T) *t); \
	public void spaceT(Collections_List,T,TrimExcess)(Collections_List(T) *t); \
	public bool spaceT(Collections_List,T,TrueForAll)(Collections_List(T) *t, System_Predicate(T) match); \
	public int_ spaceT(Collections_List,T,getCapacity)(Collections_List(T) *t); \
	public void spaceT(Collections_List,T,setCapacity)(Collections_List(T) *t, int_ value);
#define Implement_Collections_List(T, Ti) \
	Implement_TypeT(Collections_List,T,System_Object,) \
	private static int_ spaceT(Collections_List,T,s_emptyArrayLength) = 0; private static Ti spaceT(Collections_List,T,s_emptyArray)[] = { nullT(Ti) }; \
	/* Forward-declare */ \
	private void spaceT(Collections_List,T,EnsureCapacity)(Collections_List(T) *t, int_ min); \
	private static_ bool spaceT(Collections_List,T,IsCompatibleObject)(System_Object value); \
	\
	Implement_CtorT(,Collections_List,T, \
		baseCtor(,t,System_Object); \
		t->Count = 0; \
		t->Items = spaceT(Collections_List,T,s_emptyArray); \
	,) \
	Implement_CtorT(2,Collections_List,T, \
		Collections_ICollection(T) *is2; \
		if (collection == null) \
		{ \
			throw(ArgumentNullException, L"collection"); \
		} \
		is2 = as(collection,Collections_ICollection(T)); \
		if (is2 != null) \
		{ \
			int count = getVcallT(Collections_ICollection,T,is2,Count); \
			t->Items = newArray(t->Items, Ti, count); \
			vcallT(Collections_ICollection,T,is2,CopyTo, t->Items, 0); \
			t->Count = count; \
		} else { \
			t->Count = 0; \
			t->Items = newArray(t->Items, Ti, 4); \
			using(enumerator,Collections_IEnumerator(T) *enumerator = vcallT(Collections_IEnumerable,T,collection,GetEnumerator), \
				while (vcallT(Collections_IEnumerator,T,enumerator,MoveNext) == true) \
				{ \
					spaceT(Collections_List,T,Add)(t, getVcallT(Collections_IEnumerator,T,enumerator,Current)); \
				} \
			,); \
		} \
	, Collections_IEnumerable(T) *collection) \
	Implement_CtorT(3,Collections_List,T, \
		if (capacity < 0) \
		{ \
			throw(ArgumentOutOfRangeException, L"ArgumentOutOfRange_SmallCapacity"); \
		} \
		baseCtor(,t,System_Object); \
		t->Count = 0; \
		t->Items = newArray(t->Items, Ti, capacity); \
	, int_ capacity) \
	\
	/*[ Add ]*/ \
	public void spaceT(Collections_List,T,Add)(Collections_List(T) *t, Ti item) \
	{ \
		if (t->Count == array_getLength(t->Items)) \
		{ \
			spaceT(Collections_List,T,EnsureCapacity)(t, t->Count + 1); \
		} \
		t->Items[t->Count++] = item; \
		t->Version++; \
	} \
	\
	/*[ BinarySearch ]*/ \
	public int_ spaceT(Collections_List,T,BinarySearch)(Collections_List(T) *t, Ti item) \
	{ \
		return spaceT(Collections_List,T,BinarySearch3)(t, 0, t->Count, item, nullT(Collections_IComparer(T)*)); \
	} \
	/*[ BinarySearch2 ]*/ \
	public int_ spaceT(Collections_List,T,BinarySearch2)(Collections_List(T) *t, Ti item, Collections_IComparer(T) *comparer) \
	{ \
		return spaceT(Collections_List,T,BinarySearch3)(t, 0, t->Count, item, comparer); \
	} \
	/*[ BinarySearch3 ]*/ \
	public int_ spaceT(Collections_List,T,BinarySearch3)(Collections_List(T) *t, int_ index, int_ count, Ti item, Collections_IComparer(T) *comparer) \
	{ \
		if ((index < 0) || (count < 0)) \
		{ \
			throw(ArgumentOutOfRangeException, (index < 0 ? L"index" : L"count")); \
		} \
		if ((t->Count - index) < count) \
		{ \
			throw(ArgumentException, L"Argument_InvalidOffLen"); \
		} \
		return space_T(System_Array,BinarySearch,T)(t->Items, index, count, item, comparer); \
	} \
	\
	/*[ Clear ]*/ \
	public void spaceT(Collections_List,T,Clear)(Collections_List(T) *t) \
	{ \
		if (t->Count > 0) \
		{ \
			space(System_Array,Clear)(t->Items, 0, t->Count); \
			t->Count = 0; \
		} \
		t->Version++; \
	} \
	\
	/*[ Contains ]*/ \
	public bool spaceT(Collections_List,T,Contains)(Collections_List(T) *t, Ti item) \
	{ \
		int_ i, j; \
		Collections_EqualityComparer(T) *comparer; \
		if (item == nullT(Ti)) \
		{ \
			for (j = 0; j < t->Count; j++) \
			{ \
				if (t->Items[j] == nullT(Ti)) \
				{ \
					return true; \
				} \
			} \
			return false; \
		} \
		comparer = getsT(Collections_EqualityComparer,T,Default); \
		for (i = 0; i < t->Count; i++) \
		{ \
			if (vcallT(Collections_EqualityComparer,T,comparer,Equals, t->Items[i], item) == true) \
			{ \
				return true; \
			} \
		} \
		return false; \
	} \
	/*[ CopyTo ]*/ \
	public void spaceT(Collections_List,T,CopyTo)(Collections_List(T) *t, Ti array[]) \
	{ \
		spaceT(Collections_List,T,CopyTo2)(t, array, 0); \
	} \
	/*[ CopyTo2 ]*/ \
	public void spaceT(Collections_List,T,CopyTo2)(Collections_List(T) *t, Ti array[], int_ arrayIndex) \
	{ \
		space(System_Array,Copy3)(t->Items, 0, array, arrayIndex, t->Count); \
	} \
	/*[ CopyTo3 ]*/ \
	public void spaceT(Collections_List,T,CopyTo3)(Collections_List(T) *t, int_ index, Ti array[], int_ arrayIndex, int_ count) \
	{ \
		if ((t->Count - index) < count) \
		{ \
			throw(ArgumentException, L"Argument_InvalidOffLen"); \
		} \
		space(System_Array,Copy3)(t->Items, index, array, arrayIndex, count); \
	} \
	\
	/*[ EnsureCapacity ]*/ \
	private void spaceT(Collections_List,T,EnsureCapacity)(Collections_List(T) *t, int_ min) \
	{ \
		int_ itemsLength = array_getLength(t->Items); \
		if (itemsLength < min) \
		{ \
			int_ num = (itemsLength == 0 ? 4 : (itemsLength << 1)); \
			if (num < min) \
			{ \
				num = min; \
			} \
			setT(Collections_List,T,t,Capacity,num); \
		} \
	} \
	\
	/*[ Exists ]*/ \
	public bool spaceT(Collections_List,T,Exists)(Collections_List(T) *t, System_Predicate(T) match) \
	{ \
		return (spaceT(Collections_List,T,FindIndex)(t, match) != -1); \
	} \
	\
	/*[ Find ]*/ \
	public Ti spaceT(Collections_List,T,Find)(Collections_List(T) *t, System_Predicate(T) match) \
	{ \
		int_ i; \
		if (match == null) \
		{ \
			throw(ArgumentNullException, L"match"); \
		} \
		for (i = 0; i < t->Count; i++) \
		{ \
			if (match(t->Items[i]) == true) \
			{ \
				return t->Items[i]; \
			} \
		} \
		return defaultT(T,Ti); \
	} \
	\
	/*[ FindAll ]*/ \
	public Collections_List(T) *spaceT(Collections_List,T,FindAll)(Collections_List(T) *t, System_Predicate(T) match) \
	{ \
		int_ i; \
		Collections_List(T) *list; \
		if (match == null) \
		{ \
			throw(ArgumentNullException, L"match"); \
		} \
		list = new(,Collections_List(T)); \
		for (i = 0; i < t->Count; i++) \
		{ \
			if (match(t->Items[i]) == true) \
			{ \
				spaceT(Collections_List,T,Add)(list, t->Items[i]); \
			} \
		} \
		return list; \
	} \
	/*[ FindIndex ]*/ \
	public int_ spaceT(Collections_List,T,FindIndex)(Collections_List(T) *t, System_Predicate(T) match) \
	{ \
		return spaceT(Collections_List,T,FindIndex3)(t, 0, t->Count, match); \
	} \
	/*[ FindIndex2 ]*/ \
	public int_ spaceT(Collections_List,T,FindIndex2)(Collections_List(T) *t, int_ startIndex, System_Predicate(T) match) \
	{ \
		return spaceT(Collections_List,T,FindIndex3)(t, startIndex, t->Count - startIndex, match); \
	} \
	/*[ FindIndex3 ]*/ \
	public int_ spaceT(Collections_List,T,FindIndex3)(Collections_List(T) *t, int_ startIndex, int_ count, System_Predicate(T) match) \
	{ \
		int_ num, i; \
		if (startIndex > t->Count) \
		{ \
			throw(ArgumentOutOfRangeException, L"startIndex"); \
		} \
		if ((count < 0) || (startIndex > (t->Count - count))) \
		{ \
			throw(ArgumentOutOfRangeException, L"count"); \
		} \
		if (match == null) \
		{ \
			throw(ArgumentNullException, L"match"); \
		} \
		num = startIndex + count; \
		for (i = startIndex; i < num; i++) \
		{ \
			if (match(t->Items[i]) == true) \
			{ \
				return i; \
			} \
		} \
		return -1; \
	} \
	\
	/*[ FindLast ]*/ \
	public Ti spaceT(Collections_List,T,FindLast)(Collections_List(T) *t, System_Predicate(T) match) \
	{ \
		int_ i; \
		if (match == null) \
		{ \
			throw(ArgumentNullException, L"match"); \
		} \
		for (i = t->Count - 1; i >= 0; i--) \
		{ \
			if (match(t->Items[i]) == true) \
			{ \
				return t->Items[i]; \
			} \
		} \
		return defaultT(T,Ti); \
	} \
	\
	/*[ FindLastIndex ]*/ \
	public int_ spaceT(Collections_List,T,FindLastIndex)(Collections_List(T) *t, System_Predicate(T) match) \
	{ \
		return spaceT(Collections_List,T,FindLastIndex3)(t, t->Count - 1, t->Count, match); \
	} \
	/*[ FindLastIndex2 ]*/ \
	public int_ spaceT(Collections_List,T,FindLastIndex2)(Collections_List(T) *t, int_ startIndex, System_Predicate(T) match) \
	{ \
		return spaceT(Collections_List,T,FindLastIndex3)(t, startIndex, startIndex + 1, match); \
	} \
	/*[ FindLastIndex3 ]*/ \
	public int_ spaceT(Collections_List,T,FindLastIndex3)(Collections_List(T) *t, int_ startIndex, int_ count, System_Predicate(T) match) \
	{ \
		int_ num, i; \
		if (match == null) \
		{ \
			throw(ArgumentNullException, L"match"); \
		} \
		if (t->Count == 0) \
		{ \
			if (startIndex != -1) \
			{ \
				throw(ArgumentOutOfRangeException, L"startIndex"); \
			} \
		} \
		else if (startIndex >= t->Count) \
		{ \
			throw(ArgumentOutOfRangeException, L"startIndex"); \
		} \
		if ((count < 0) || (((startIndex - count) + 1) < 0)) \
		{ \
			throw(ArgumentOutOfRangeException, L"count"); \
		} \
		num = startIndex - count; \
		for (i = startIndex; i > num; i--) \
		{ \
			if (match(t->Items[i]) == true) \
			{ \
				return i; \
			} \
		} \
		return -1; \
	} \
	\
	/*[ ForEach ]*/ \
	public void spaceT(Collections_List,T,ForEach)(Collections_List(T) *t, System_Action(T) action) \
	{ \
		int_ i; \
		if (action == null) \
		{ \
			throw(ArgumentNullException, L"match"); \
		} \
		for (i = 0; i < t->Count; i++) \
		{ \
			action(t->Items[i]); \
		} \
	} \
	\
	/*[ GetRange ]*/ \
	public Collections_List(T) *spaceT(Collections_List,T,GetRange)(Collections_List(T) *t, int_ index, int_ count) \
	{ \
		Collections_List(T) *list; \
		if ((index < 0) || (count < 0)) \
		{ \
			throw(ArgumentOutOfRangeException, (index < 0 ? L"index" : L"count")); \
		} \
		if ((t->Count - index) < count) \
		{ \
			throw(ArgumentException, L"Argument_InvalidOffLen"); \
		} \
		list = new(3,Collections_List(T),count); \
		space(System_Array,Copy3)(t->Items, index, list->Items, 0, count); \
		list->Count = count; \
		return list; \
	} \
	\
	/*[ IndexOf ]*/ \
	public int_ spaceT(Collections_List,T,IndexOf)(Collections_List(T) *t, Ti item) \
	{ \
		return space_T(System_Array,IndexOf3,T)(t->Items, item, 0, t->Count); \
	} \
	/*[ IndexOf2 ]*/ \
	public int_ spaceT(Collections_List,T,IndexOf2)(Collections_List(T) *t, Ti item, int_ index) \
	{ \
		if (index > t->Count) \
		{ \
			throw(ArgumentOutOfRangeException, L"index"); \
		} \
		return space_T(System_Array,IndexOf3,T)(t->Items, item, index, t->Count - index); \
	} \
	/*[ IndexOf3 ]*/ \
	public int_ spaceT(Collections_List,T,IndexOf3)(Collections_List(T) *t, Ti item, int_ index, int_ count) \
	{ \
		if (index > t->Count) \
		{ \
			throw(ArgumentOutOfRangeException, L"index"); \
		} \
		if ((count < 0) || (index > (t->Count - count))) \
		{ \
			throw(ArgumentOutOfRangeException, L"count"); \
		} \
		return space_T(System_Array,IndexOf3,T)(t->Items, item, index, count); \
	} \
	\
	/*[ Insert ]*/ \
	public void spaceT(Collections_List,T,Insert)(Collections_List(T) *t, int_ index, Ti item) \
	{ \
		if (index > t->Count) \
		{ \
			throw(ArgumentOutOfRangeException, L"index"); \
		} \
		if (t->Count == array_getLength(t->Items)) \
		{ \
			spaceT(Collections_List,T,EnsureCapacity)(t, t->Count + 1); \
		} \
		if (index < t->Count) \
		{ \
			space(System_Array,Copy3)(t->Items, index, t->Items, index + 1, t->Count - index); \
		} \
		t->Items[index] = item; \
		t->Count++; \
		t->Version++; \
	} \
	\
	/*[ IsCompatibleObject ]*/ \
	private static_ bool spaceT(Collections_List,T,IsCompatibleObject)(System_Object value) \
	{ \
		/* \
		if (((value is T) == false) && ((value != null) || (typeof(T).IsValueType == true))) \
		{ \
		    return false; \
		} \
		*/ \
		return true; \
	} \
	\
	/*[ LastIndexOf ]*/ \
	public int_ spaceT(Collections_List,T,LastIndexOf)(Collections_List(T) *t, Ti item) \
	{ \
		return spaceT(Collections_List,T,LastIndexOf3)(t, item, t->Count - 1, t->Count); \
	} \
	/*[ LastIndexOf2 ]*/ \
	public int_ spaceT(Collections_List,T,LastIndexOf2)(Collections_List(T) *t, Ti item, int_ index) \
	{ \
		if (index >= t->Count) \
		{ \
			throw(ArgumentOutOfRangeException, L"index"); \
		} \
		return spaceT(Collections_List,T,LastIndexOf3)(t, item, index, index + 1); \
	} \
	/*[ LastIndexOf3 ]*/ \
	public int_ spaceT(Collections_List,T,LastIndexOf3)(Collections_List(T) *t, Ti item, int_ index, int_ count) \
	{ \
		if (t->Count == 0) \
		{ \
			return -1; \
		} \
		if ((index < 0) || (count < 0)) \
		{ \
			throw(ArgumentOutOfRangeException, (index < 0 L"index" : L"count")); \
		} \
		if ((index >= t->Count) || (count > (index + 1))) \
		{ \
			throw(ArgumentOutOfRangeException, (index >= t->Count ? L"index" : L"count")); \
		} \
		return space_T(System_Array,LastIndexOf3,T)(t->Items, item, index, count); \
	} \
	\
	/*[ RemoveAll ]*/ \
	public int_ spaceT(Collections_List,T,RemoveAll)(Collections_List(T) *t, System_Predicate(T) match) \
	{ \
		int_ index, num2, num3; \
		if (match == null) \
		{ \
			throw(ArgumentNullException, L"match"); \
		} \
		index = 0; \
		while ((index < t->Count) && (match(t->Items[index]) == false)) \
		{ \
			index++; \
		} \
		if (index >= t->Count) \
		{ \
			return 0; \
		} \
		num2 = index + 1; \
		while (num2 < t->Count) \
		{ \
			while ((num2 < t->Count) && (match(t->Items[num2]) == true)) \
			{ \
				num2++; \
			} \
			if (num2 < t->Count) \
			{ \
				t->Items[index++] = t->Items[num2++]; \
			} \
		} \
		space(System_Array,Clear)(t->Items, index, t->Count - index); \
		num3 = t->Count - index; \
		t->Count = index; \
		t->Version++; \
		return num3; \
	} \
	\
	/*[ RemoveAt ]*/ \
	public void spaceT(Collections_List,T,RemoveAt)(Collections_List(T) *t, int_ index) \
	{ \
		if (index >= t->Count) \
		{ \
			throw(ArgumentOutOfRangeException, null); \
		} \
		t->Count--; \
		if (index < t->Count) \
		{ \
			space(System_Array,Copy3)(t->Items, index + 1, t->Items, index, t->Count - index); \
		} \
		t->Items[t->Count] = defaultT(T,Ti); \
		t->Version++; \
	} \
	\
	/*[ RemoveRange ]*/ \
	public void spaceT(Collections_List,T,RemoveRange)(Collections_List(T) *t, int_ index, int_ count) \
	{ \
		if ((index < 0) || (count < 0)) \
		{ \
			throw(ArgumentOutOfRangeException, (index < 0 ? L"index" : L"count")); \
		} \
		if ((t->Count - index) < count) \
		{ \
			throw(ArgumentException, L"Argument_InvalidOffLen"); \
		} \
		if (count > 0) \
		{ \
			t->Count -= count; \
			if (index < t->Count) \
			{ \
				space(System_Array,Copy3)(t->Items, index + count, t->Items, index, t->Count - index); \
			} \
			space(System_Array,Clear)(t->Items, t->Count, count); \
			t->Version++; \
		} \
	} \
	\
	/*[ Reverse ]*/ \
	public void spaceT(Collections_List,T,Reverse)(Collections_List(T) *t) \
	{ \
		spaceT(Collections_List,T,Reverse2)(t, 0, t->Count); \
	} \
	/*[ Reverse2 ]*/ \
	public void spaceT(Collections_List,T,Reverse2)(Collections_List(T) *t, int_ index, int_ count) \
	{ \
		if ((index < 0) || (count < 0)) \
		{ \
			throw(ArgumentOutOfRangeException, (index < 0 ? L"index" : L"count")); \
		} \
		if ((t->Count - index) < count) \
		{ \
			throw(ArgumentException,L"Argument_InvalidOffLen"); \
		} \
		space(System_Array,Reverse2)(t->Items, index, count); \
		t->Version++; \
	} \
	\
	/*[ Sort ]*/ \
	public void spaceT(Collections_List,T,Sort)(Collections_List(T) *t) \
	{ \
		spaceT(Collections_List,T,Sort4)(t, 0, t->Count, nullT(Collections_IComparer(T)*)); \
	} \
	/*[ Sort2 ]*/ \
	public void spaceT(Collections_List,T,Sort2)(Collections_List(T) *t, Collections_IComparer(T) *comparer) \
	{ \
		spaceT(Collections_List,T,Sort4)(t, 0, t->Count, comparer); \
	} \
	/*[ Sort3 ]*/ \
	public void spaceT(Collections_List,T,Sort3)(Collections_List(T) *t, System_Comparison(T) comparison) \
	{ \
		if (comparison == null) \
		{ \
			throw(ArgumentNullException, L"match"); \
		} \
		if (t->Count > 0) \
		{ \
			Collections_IComparer(T) *comparer = asNew(Collections_IComparer(T),,System_ArrayXFunctorComparer(T),comparison); \
			space_T(System_Array,Sort5,T)(t->Items, 0, t->Count, comparer); \
		} \
	} \
	/*[ Sort4 ]*/ \
	public void spaceT(Collections_List,T,Sort4)(Collections_List(T) *t, int_ index, int_ count, Collections_IComparer(T) *comparer) \
	{ \
		if ((index < 0) || (count < 0)) \
		{ \
			throw(ArgumentOutOfRangeException, (index < 0 ? L"index" : L"count")); \
		} \
		if ((t->Count - index) < count) \
		{ \
			throw(ArgumentException, L"Argument_InvalidOffLen"); \
		} \
		space_T(System_Array,Sort5,T)(t->Items, index, count, comparer); \
		t->Version++; \
	} \
	\
	/*[ ToArray ]*/ \
	public Ti *spaceT(Collections_List,T,ToArray)(Collections_List(T) *t) \
	{ \
		Ti *destinationArray = newArray(destinationArray, Ti, t->Count); \
		space(System_Array,Copy3)(t->Items, 0, destinationArray, 0, t->Count); \
		return destinationArray; \
	} \
	\
	/*[ TrimExcess ]*/ \
	public void spaceT(Collections_List,T,TrimExcess)(Collections_List(T) *t) \
	{ \
		int_ num = (int_)(array_getLength(t->Items) * 0.9); \
		if (t->Count < num) \
		{ \
			setT(Collections_List,T,t,Capacity,t->Count); \
		} \
	} \
	\
	/*[ TrueForAll ]*/ \
	public bool spaceT(Collections_List,T,TrueForAll)(Collections_List(T) *t, System_Predicate(T) match) \
	{ \
		int_ i; \
		if (match == null) \
		{ \
			throw(ArgumentNullException, L"match"); \
		} \
		for (i = 0; i < t->Count; i++) \
		{ \
			if (match(t->Items[i]) == false) \
			{ \
				return false; \
			} \
		} \
		return true; \
	} \
	\
	/*[ VerifyValueType ]*/ \
	private static void spaceT(Collections_List,T,VerifyValueType)(Collections_List(T) *t, System_Object value) \
	{ \
		if (spaceT(Collections_List,T,IsCompatibleObject)(value) == false) \
		{ \
			throw(WrongValueTypeArgumentException, L"value"); \
		} \
	} \
	\
	/*[ Capacity ]*/ \
	public int_ spaceT(Collections_List,T,getCapacity)(Collections_List(T) *t) \
	{ \
		return array_getLength(t->Items); \
	} \
	public void spaceT(Collections_List,T,setCapacity)(Collections_List(T) *t, int_ value) \
	{ \
        if (value != array_getLength(t->Items)) \
        { \
            if (value < t->Count) \
            { \
                throw(ArgumentOutOfRangeException, L"value"); \
            } \
			if (t->Items != spaceT(Collections_List,T,s_emptyArray)) \
			{ \
				freeArray(t->Items); \
			} \
            if (value > 0) \
            { \
                Ti *destinationArray = newArray(destinationArray,Ti,value); \
                if (t->Count > 0) \
                { \
                    space(System_Array,Copy3)(t->Items, 0, destinationArray, 0, t->Count); \
                } \
                t->Items = destinationArray; \
            } \
            else \
            { \
                t->Items = spaceT(Collections_List,T,s_emptyArray); \
            } \
        } \
    }


//+ Generic-Method Definitions
///<summary>
/// ConvertAll
///</summary>
#define Declare_Collections_List_ConvertAll(T, Ti, TOutput, TiOutput) \
	/* Forward-declare */ \
	public Collections_List(TOutput) *spaceT_T(Collections_List,T,ConvertAll,TOutput)(Collections_List(T) *t, System_Converter(T, TOutput) *converter);
#define Implement_Collections_List_ConvertAll(T, Ti, TOutput, TiOutput) \
	/*[ ConvertAll ]*/ \
	public Collections_List(TOutput) *spaceT_T(Collections_List,T,ConvertAll,TOutput)(Collections_List(T) *t, System_Converter(T, TOutput) *converter) \
	{ \
		int_ i; \
		Collections_List(TOutput) *list; \
		if (converter == null) \
		{ \
			throw(ArgumentNullException, L"converter"); \
		} \
		list = new(Collections_List(TOutput),t->Size); \
		for (i = 0; i < t->Count; i++) \
		{ \
			l->Items[i] = converter(t->Items[i]); \
		} \
		list->Count = t->Count; \
		return list; \
	}

#endif /* __Collections_List_H__ */
